/**
 * Created by yangfan on 2016/11/23.
 */
define(['config/appregister',
		"i18n",
		"jquery",
		"business/myResume/services/httpServices",
		"business/services/utils",	
		],
function(app,i18n,$,httpServices,utils,constant){
    app.directive("projectExpression",function(){
        return {
            templateUrl:"app/business/myResume/templates/projectionExpression.html",
            scope:{
                conf:"=confData"
            },
            link:function(scope,element,attrs){
             scope.i18n = i18n;   
             scope.curI18n = i18n.resume;
             scope.isShow  = {
             	workStart:false,
             	workEnd:false,
             };
              var selectStyleSmall = {"background-color":"#fff", "width":"90px"},
        		  selectStyleBig = {"background-color":"#fff", "width":"200px"};
        	
        function getValidateArr(form,topKey){
            if(!angular.isObject(form)){
                return ;
            };
            var itemArr = [];
            for(var key in form ){
                if(key.indexOf("$")<0){
                    itemArr.push(key);
                }
            }
           return  getValidateItem(topKey,itemArr);
        }
        function getValidateItem(topKey,itemArr){
            if( toString.call(itemArr) != '[object Array]'){
                return
            }
            var flag =false;
            itemArr.forEach(function(item ){
                if(scope[topKey][item].$invalid == true){
                    scope[topKey][item].$pristine = false;
                    flag = true;
                }
            });
            return flag ;
        }
        	 
        	 scope.showComponent = function(key){
           		  scope[key].isShow = !scope[key].isShow ;
        		};
        	
        	 scope.changeShow = function(key){
            	if(scope.isShow.hasOwnProperty(key)){
                scope.isShow[key] = !scope.isShow[key];
           	 }
        };
                scope.curentCalendarLeave = function(){
                    scope.isShow.workStart = false ;
                    scope.isShow.workEnd = false ;
                }
                
         //项目经验
        scope.companyName = "";
        scope.projectName = "";
        scope.playRole = "";
        scope.introduction = "";
       		  //工作时间
      	scope.projectExpression = {
            startValue:"",
            endValue:"",
            isShow:false
        	};
        scope.projectExpressionSubmit = function(){
        		if(scope.projectExpressionForm.$invalid){
                topKey = "projectExpressionForm";
                var flag = getValidateArr(scope.projectExpressionForm,topKey);

            }else{
            	var guildObj = {
            		dictId:[]
            	};
            	var fcObj = {
            		dictId:[]
            	};
            	
            	var jsonStr = [
            	{
            	}
            	];
            	jsonStr = JSON.stringify(jsonStr);
            	var req = {
            		type:"POST",
            		uri:"saveProjectExp",
            		data:{
                        startTime:utils.YMD2YMD(scope.projectExpression.startValue),
                        endTime:utils.YMD2YMD(scope.projectExpression.endValue),
                        peName:scope.projectName,
                        companyName:scope.companyName,
                        roleName:scope.playRole,
                        introduction:scope.introduction,
            		}
            	};
                    if(scope.isUpdate == true){
                        req.uri =  "updateProjectExp";
                        req.data.peId = scope.conf.peId;
                        req.data.cvdId = scope.conf.cvdId;
                    }
            	 httpServices.getCvdatabases(req).success(function(response){
                     if(response.msg == "success"){
                         Back(0)

                     }
                }).error(function(response){
                     Back(0)
                })
            	
            }
        	};
                scope.cancelProExpressionSubmit = function(){
                    Back(1)
                };
                function Back(type){
                    if(scope.isUpdate == true){
                        var obj = {
                            key1:"showProjectExpression",
                            key2:"showForm",
                            type:type
                        }
                    }else{
                        var obj = {
                            key1:"isInitAddNew",
                            key2:"projectExpression",
                            type:type
                        }
                    }
                    scope.$emit("reGetResumeInfo",obj);
                }
                scope.isUpdate = false ;
         function init (){
             if(scope.conf){
               fillData()
             }
         }
         function fillData(){
             scope.projectExpression.startValue = utils.getYMD(scope.conf.startValue);
             scope.projectExpression.endValue = utils.getYMD(scope.conf.endValue);
             scope.projectName = scope.conf.peName;
             scope.companyName = scope.conf.companyName;
             scope.playRole = scope.conf.roleName;
             scope.introduction = scope.conf.introduction;
             scope.isUpdate = true;
         }
                init();



            }
        }
    })
});